<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>异动原因设置</span>
            </div>
            <div>
                <el-row class="border-bottom" style="height:56px;">
                    <el-col :span="24">
                        <div class="float-right">
                            <el-button plain @click="deleteReasons()">{{ $t('message.delete') }}</el-button>
                            <el-button type="primary" @click="addReason()">{{ $t('message.add') }}</el-button>
                        </div>
                    </el-col>
                </el-row>
                <el-table :data="reasons" size="mini" @selection-change="handlerSelectionChange">
                    <el-table-column type="selection" stripe/>
                    <el-table-column type="index" label="编号" :index="indexMethod"/>
                    <el-table-column label="异动类型">
                        <template slot-scope="scope">{{ scope.row.typeName }}</template>
                    </el-table-column>
                    <el-table-column label="异动原因">
                        <template slot-scope="scope">{{ scope.row.name }}</template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="mini"
                                @click="editReason(scope.row)"
                            >{{ $t('message.edit') }}</el-button>
                            <el-button
                                type="text"
                                size="mini"
                                @click="deleteReason(scope.row)"
                            >{{ $t('message.delete') }}</el-button>
                            <!-- <el-button type="text" @click="editTask(scope.row)" size="mini">编辑</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>
                <div class="edu-pagination">
                    <div class="float-right">
                        <el-pagination
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20,50,100,500,1000]"
                            :page-size="queryForm.pageSize_"
                            :total="totalCount"
                            layout="total, sizes, prev, pager, next, jumper"
                            style="float: right;margin:10px 60px 0 0;"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                        />
                    </div>
                </div>
            </div>
        </el-card>
        <!--删除原因弹窗-->
        <delete-dialog
            :modal-title="deleteReasonDialog.title"
            title="确认删除以下信息"
            :show-dialog="deleteReasonDialog.showDialog"
            :data="deleteReasonDialog.reasons"
            :columns="deleteReasonDialog.columns"
            :ok-call-back="handlerDeleteReasons"
            :handle-close="closeDeleteReasonDialog"
        />
        <!--添加原因弹窗-->
        <el-dialog
            v-if="reasonDialog.showDialog"
            :title.sync="reasonDialog.title"
            :visible.sync="reasonDialog.showDialog"
            width="40%"
            :before-close="handlereasonDialogClose"
        >
            <el-form :model="reasonDialog.formData" :inline="true" :rules="reasonDialog.rules">
                <el-form-item label="异动原因" :label-width="reasonDialog.formLabelWidth" prop="name">
                    <el-input v-model="reasonDialog.formData.name"></el-input>
                </el-form-item>
                <el-form-item label="英文" :label-width="reasonDialog.formLabelWidth">
                    <el-input v-model="reasonDialog.formData.nameEn"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="reasonDialog.showDialog = false">取 消</el-button>
                <el-button type="primary" @click="handlerAddReason">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import * as API from "common/src/api/studentStatus";
import deleteDialog from "./../DeleteItems";
export default {
    components: { deleteDialog },
    props: {
        typeId: {
            required: true,
            type: String
        },
        typeName: {
            required: true,
            type: String
        }
    },
    data() {
        let checkNameExist = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("输入不能为空"));
            }
            API.checkNameExist(value).then(response => {
                let { code, data } = response;
                if (code === 200 && data) {
                    callback(new Error("名称已存在"));
                }
            });
        };
        return {
            reasons: [],
            queryForm: {
                pageNum_: 1,
                pageSize_: 20
            },
            totalCount: 0,
            selectedReasons: [],
            deleteReasonDialog: {
                title: "删除异动原因",
                showDialog: false,
                reasons: [],
                columns: [
                    { label: "异动类型", key: "typeName" },
                    { label: "异动原因", key: "name" }
                ]
            },
            reasonDialog: {
                title: "添加原因",
                showDialog: false,
                formData: {
                    name: "",
                    nameEn: ""
                },
                formLabelWidth: "100px",
                rules: {
                    name: [
                        { validator: checkNameExist, trigger: "blur" },
                        { required: true, trigger: "blur" }
                    ]
                }
            }
        };
    },
    mounted() {
        this.getReasons({
            ...this.queryForm,
            typeId: this.typeId
        });
    },
    methods: {
        getReasons(params) {
            API.getReasons(params).then(response => {
                if (response.code === 200) {
                    response.data.list.forEach(element => {
                        element.typeName = this.typeName;
                    });
                    this.reasons = response.data.list;
                    this.totalCount = response.data.total_;
                } else throw response.code;
            });
        },
        handlerSelectionChange(val) {
            this.selectedReasons = val;
        },
        handlereasonDialogClose(done) {
            this.reasonDialog.showDialog = false;
            done();
        },
        addReason() {
            this.reasonDialog.title = "添加原因";
            this.reasonDialog.formData = { name: "", nameEn: "" };
            this.reasonDialog.showDialog = true;
        },
        handlerAddReason() {
            let promise, message;
            if (this.reasonDialog.formData.id) {
                promise = API.editReason({
                    ...this.reasonDialog.formData,
                    typeId: this.typeId
                });
                message = "修改成功";
            } else {
                promise = API.addReason({
                    ...this.reasonDialog.formData,
                    typeId: this.typeId
                });
                message = "添加成功";
            }
            promise.then(response => {
                if (response.code === 200) {
                    this.$message({
                        message,
                        type: "success"
                    });
                    this.reasonDialog.showDialog = false;
                    this.getReasons({
                        ...this.queryForm,
                        typeId: this.typeId
                    });
                }
            });
        },
        editReason(row) {
            this.reasonDialog.title = "修改原因";
            let { id, name, nameEn } = row;
            this.reasonDialog.formData = { id, name, nameEn };
            this.reasonDialog.showDialog = true;
        },
        deleteReason(val) {
            this.deleteReasonDialog.reasons = [val];
            this.deleteReasonDialog.showDialog = true;
        },
        deleteReasons() {
            let { selectedReasons } = this;
            if (selectedReasons.length === 0) {
                return this.$message({
                    message: "请选择至少一条数据删除",
                    type: "error"
                });
            }
            this.deleteReasonDialog.reasons = selectedReasons;
            this.deleteReasonDialog.showDialog = true;
        },
        closeDeleteReasonDialog(done) {
            this.deleteReasonDialog.showDialog = false;
            done();
        },
        handlerDeleteReasons() {
            //发送请求
            let params = this.selectedReasons.map(item => item.id);
            API.deleteReason(params)
                .then(
                    response => {
                        if (response.code === 200) {
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            });
                            this.getReasons();
                        }
                    },
                    xhr => {}
                )
                .finally(() => {
                    this.closeDeleteReasonDialog();
                });
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val;
            this.getReasons();
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val;
            this.getReasons();
        },
        indexMethod(index) {
            return index + 1;
        }
    }
};
</script>
<style>
</style>
